/**
 * Created by Administrator on 2016/10/16.
 */
angular.module('commonModel', []).service('$commonService', ['$http', function ($http) {
    /**
     * 登录状态查询,force时,强制服务器验证
     */
    this.checkAdminLoginState = function (force, success, fail) {
        if (!LOCAL_STORE.context.session || !LOCAL_STORE.context.session.user) {
            fail();
        }
        if (!force) {
            //构造http返回的统一格式
            var tmpData = {};
            tmpData.data = {result:true};
            success(tmpData);
        }
        else {
            $http({
                method: 'GET',
                url: '/admin/loginState/',
                params: ''
            }).then(success, fail);
        }

    };

    /*退出*/
    this.logout= function(success,fail) {
        $http.post('/admin/logout/').then(success,fail);
    }

    this.getContentEditor = function (domId) {
        var domId = domId || 'editor-trigger';
        var editor = new wangEditor(domId);
        editor.config.menuFixed = false;
        //上传图片
        editor.config.menus = [
            'bold',
            'underline',
            'italic',
            'strikethrough',
            'eraser',
            'forecolor',
            'bgcolor',
            '|',
            'quote',
            'fontfamily',
            'fontsize',
            'head',
            'unorderlist',
            'orderlist',
            'alignleft',
            'aligncenter',
            'alignright',
            '|',
            'link',
            'unlink',
            'table',
            /*        'emotion',*/
            '|',
            'img',
            'video',
            /*        'location',
             'insertcode',*/
            '|',
            'undo',
            'redo',
            'fullscreen'
        ];

        //$uploadContent
        editor.config.customUpload = true;

        var uploadInit = function () {
            var editor = this;
            // 编辑器中，触发选择图片的按钮的id
            var btnId = editor.customUploadBtnId;
            // 编辑器中，触发选择图片的按钮的父元素的id
            var containerId = editor.customUploadContainerId;
            var $input = $('<input type="file" accept="image/*" />');
            var containerInput = $('<div style="visibility:hidden;"></div>');
            containerInput.append($input);
            editor.customUploadInputBtn = $input;
            $('body').append(containerInput);

            // onchange 事件
            $input.on('change', function (e) {
                var file = e.currentTarget.files[0];
                if (file) {
                    var avFile = new AV.File(file.name, file);
                    avFile.save().then(function () {
                        editor.command(null, 'insertHtml', '<img src="' + avFile.url() + '" style="max-width:100%;"/>');
                        console.log("上传成功");
                        editor.customUploadInputBtn.val('');
                    }, function (error) {
                        console.log("上传失败");
                        editor.customUploadInputBtn.val('');
                    })
                } else {
                    console.log('doesnot select file');
                    editor.customUploadInputBtn.val('');
                }
            });

            // 选择本地文件，上传
            $('#' + btnId).click(function (e) {
                $input.click();
                e.preventDefault();
            });
        };

        editor.config.customUploadInit = uploadInit;
        editor.create();
        return editor;
    }

}]);